/**
 * 
 */
package org.swing.utility.jai.html;

import org.swing.utility.jai.imp.HTMLRenderer;

/**
 * @author lqnhu
 *
 */
public class HTMLBuilder {
	private StringBuffer sb = new StringBuffer();

	public HTMLBuilder() {
		sb.append("<html><body>");
	}

	public void title(String title) {
		sb.append("<h2>").append(title).append("</h2><hr>");
	}

	public void dataLine(String label, Object value) {
		sb.append("<b>").append(label).append(":</b> ").append(render(value))
				.append("<br>");
	}

	public String getHtml() {
		sb.append("</html></body>");
		String result = sb.toString();
		sb = null;
		return result;
	}

	public static String render(Object value) {
		if (value == null)
			return "-";
		String result = null;
		for (HTMLRenderer renderer : HTMLRenderers.getRenderers()) {
			if (renderer.canRender(value))
				result = renderer.render(value);
		}
		if (result == null)
			result = value.toString();
		return result.replace("\n", "<br>");
	}
}
